package com.ivan.lightcontrollerdemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.ivan.lightcontrollerdemo.BleService.LocalBinder;
import com.ivan.lightcontrollerdemo.R;

public class MainActivity extends Activity {
	private SeekBar red_bar, green_bar, blue_bar, white_bar;
	protected static final String EXTRA_NAME = "name";
	protected static final String EXTRA_ID = "id";
	BleService mBleService;
	String alertCont = "ac11111111";
	String cancelAlertCont = "ac00000000";
	String lightCntStr = "adffffffff";
	boolean alertState = false;
	ServiceConnection sConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBleService = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mBleService = ((LocalBinder) service).getBleService();
			connect();
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		bindService(new Intent(this, BleService.class), sConnection,
				BIND_AUTO_CREATE);
		initView();
	}

	private void initView() {
		red_bar = (SeekBar) findViewById(R.id.red_bar);
		green_bar = (SeekBar) findViewById(R.id.green_bar);
		blue_bar = (SeekBar) findViewById(R.id.blue_bar);
		white_bar = (SeekBar) findViewById(R.id.white_bar);
		red_bar.setMax(255);
		green_bar.setMax(255);
		blue_bar.setMax(255);
		white_bar.setMax(255);
		red_bar.setOnSeekBarChangeListener(new SeekBarListener(0));
		green_bar.setOnSeekBarChangeListener(new SeekBarListener(1));
		blue_bar.setOnSeekBarChangeListener(new SeekBarListener(2));
		white_bar.setOnSeekBarChangeListener(new SeekBarListener(3));
	}

	private void connect() {
		Intent it = getIntent();
		String address = it.getStringExtra(EXTRA_ID);
		mBleService.connect(address);
	}
		@Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();
			mBleService.closeGatt();
			unbindService(sConnection);
		}
	public void alert(View v) {
		if (!alertState) {
			mBleService.writeCharacteristic(mBleService.SERVICE_UUID,
					mBleService.CHARETERISTIC_WRITEABLE_UUID, alertCont);
		} else {
			mBleService.writeCharacteristic(mBleService.SERVICE_UUID,
					mBleService.CHARETERISTIC_WRITEABLE_UUID, cancelAlertCont);
		}
		Button bt = (Button) findViewById(R.id.bt_alert);
		alertState = !alertState;
		bt.setText(alertState ? "off" : "on");
	}

	class SeekBarListener implements OnSeekBarChangeListener {
		int index;

		public SeekBarListener(int index) {
			this.index = index;
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			int finalProgress = 255 - progress;
			String finalCntStr = "";
			switch (index) {
			case 0:
				finalCntStr = UtilPaser.replace(2, 3, lightCntStr,
						finalProgress);
				break;
			case 1:
				finalCntStr = UtilPaser.replace(4, 5, lightCntStr,
						finalProgress);
				break;
			case 2:
				finalCntStr = UtilPaser.replace(6, 7, lightCntStr,
						finalProgress);
				break;
			case 3:
				finalCntStr = UtilPaser.replace(8, 9, lightCntStr,
						finalProgress);
				break;
			}
			mBleService.writeCharacteristic(BleService.SERVICE_UUID,
					BleService.CHARETERISTIC_WRITEABLE_UUID,
					lightCntStr = finalCntStr);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

	}
}
